import 'dart:convert' show json;

Type typeOf<T>() => T;
T asT<T>(dynamic value) { 
  if (value is T) {
    return value;
  }
  return null;
}

class MessageContext<T> {
  T body;
  String action;
  String roomCode;
  String userId;
  String code;
  String messageType;
  T t;
  String tips;
  MessageContext({
    this.body,
    this.action,
    this.roomCode,
    this.userId,
    this.code,
    this.messageType,
    this.tips,
  });

  factory MessageContext.fromJson(Map<String, dynamic> jsonRes) =>
      jsonRes == null
          ? null
          : MessageContext(
              body: asT<T>(jsonRes['body']),
              action: asT<String>(jsonRes['action']),
              roomCode: asT<Object>(jsonRes['roomCode']),
              userId: asT<Object>(jsonRes['userId']),
              code: asT<String>(jsonRes['code']),
              messageType: asT<Object>(jsonRes['messageType']),
              tips: asT<Object>(jsonRes['tips']),
            );

  Map<String, dynamic> toJson() => <String, dynamic>{
        'body': body,
        'action': action,
        'roomCode': roomCode,
        'userId': userId,
        'code': code,
        'messageType': messageType,
        'tips': tips,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}
